import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.*;


/**
 * Servlet implementation class RSSService
 */
public class RSSService extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public RSSService() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		getFeed(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	private void getFeed(HttpServletRequest request, HttpServletResponse response) throws IOException {

		
		//------------------------------------------------------------//
	//  JavaGetUrl.java:                                          //
	//------------------------------------------------------------//
	//  A Java program that demonstrates a procedure that can be  //
	//  used to download the contents of a specified URL.         //
	//------------------------------------------------------------//
	//  Code created by Developer's Daily                         //
	//  http://www.DevDaily.com                                   //
	//------------------------------------------------------------//

	      //-----------------------------------------------------//
	      //  Step 1:  Start creating a few objects we'll need.
	      //-----------------------------------------------------//

	      URL u;
	      InputStream is = null;
	      DataInputStream dis;
	      String s;
	      PrintWriter w = response.getWriter();
	      
	      try {

	         //------------------------------------------------------------//
	         // Step 2:  Create the URL.                                   //
	         //------------------------------------------------------------//
	         // Note: Put your real URL here, or better yet, read it as a  //
	         // command-line arg, or read it from a file.                  //
	         //------------------------------------------------------------//

	    	 String encodedUrl = request.getParameter("url");
	         u = new URL(URLDecoder.decode(encodedUrl, "UTF-8"));

	         //----------------------------------------------//
	         // Step 3:  Open an input stream from the url.  //
	         //----------------------------------------------//

	         is = u.openStream();         // throws an IOException

	         //-------------------------------------------------------------//
	         // Step 4:                                                     //
	         //-------------------------------------------------------------//
	         // Convert the InputStream to a buffered DataInputStream.      //
	         // Buffering the stream makes the reading faster; the          //
	         // readLine() method of the DataInputStream makes the reading  //
	         // easier.                                                     //
	         //-------------------------------------------------------------//

	         dis = new DataInputStream(new BufferedInputStream(is));

	         //------------------------------------------------------------//
	         // Step 5:                                                    //
	         //------------------------------------------------------------//
	         // Now just read each record of the input stream, and print   //
	         // it out.  Note that it's assumed that this problem is run   //
	         // from a command-line, not from an application or applet.    //
	         //------------------------------------------------------------//
	         response.setContentType("application/xml");
	         
	         while ((s = dis.readLine()) != null) {
	            w.println(s);
	         }

	         w.flush();
	      } catch (MalformedURLException mue) {

	         System.out.println("Ouch - a MalformedURLException happened.");
	         mue.printStackTrace();
	         System.exit(1);

	      } catch (IOException ioe) {

	         System.out.println("Oops- an IOException happened.");
	         ioe.printStackTrace();
	         System.exit(1);

	      } finally {

	         //---------------------------------//
	         // Step 6:  Close the InputStream  //
	         //---------------------------------//

	         try {
	            is.close();
	         } catch (IOException ioe) {
	            // just going to ignore this one
	         }

	      } // end of 'finally' clause
	}
}
